package net.csdn.business.mirror.mapper;

import com.baomidou.mybatisplus.annotation.InterceptorIgnore;
import com.baomidou.mybatisplus.core.mapper.BaseMapper;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import net.csdn.business.common.domain.mirror.AddMirrorImportDataDTO;
import net.csdn.business.common.domain.mirror.ProjectMirrorDataQueryDto;
import net.csdn.business.common.domain.query.UserTipsRecordQuery;
import net.csdn.business.common.domain.query.mirror.MirrorPageQuery;
import net.csdn.business.common.domain.vo.mirror.ProjectMirrorDataVO;
import net.csdn.business.common.domain.vo.user.UserTipsOrgRecordVO;
import net.csdn.business.mirror.entity.bean.ProjectMirrorData;
import org.apache.ibatis.annotations.Insert;
import org.apache.ibatis.annotations.MapKey;
import org.apache.ibatis.annotations.Param;
import org.apache.ibatis.annotations.Select;
import org.springframework.stereotype.Repository;

import java.util.List;
import java.util.Map;

@Repository
public interface ProjectMirrorDataMapper extends BaseMapper<ProjectMirrorData> {
    @Insert("<script> " +
            "INSERT INTO project_mirror_data (repo_path, full_path, creator, creator_id, target_org, target_org_id, token, user_type, type, is_sync,visibility_level,description ) VALUES " +
            "<foreach item='item' index='index' collection='imports' open='(' separator='), (' close=')'> " +
            "#{item.repoPath}, #{item.fullPath}, #{creator}, #{creatorId}, #{item.orgName},#{item.orgId},#{token},#{userType},#{type},#{isSync},#{item.visibilityLevel} ,#{item.description} " +
            "</foreach>" +
            "</script>")
    int batchImportInsert(@Param("creator") String creator,
                          @Param("creatorId") String creatorId,
                          @Param("token") String token,
                          @Param("userType") int userType,
                          @Param("type") int type,
                          @Param("isSync") int isSync,
                          @Param("imports") List<AddMirrorImportDataDTO.Info> imports);





    @MapKey("repoPath")
    @Select("<script>" +
            " select * from project_mirror_data where type= #{type} and creator_id= #{creatorId} and repo_path in "+
            "<foreach item='item' index='index' collection='repoPaths' open='(' separator=', ' close=')'>" +
            " #{item} " +
            "</foreach>" +
            "</script>")
    Map<String, ProjectMirrorData> selectMirrorDataMap(@Param("type")Integer type,@Param("creatorId")String creatorId,@Param("repoPaths") List<String> repoPaths);



    @Select("<script>" +
            " select * from project_mirror_data where 1=1 " +
            "<if test='query.sourcePath != null and query.sourcePath!=\"\"' >" +
            " and repo_path like concat('%', #{query.sourcePath}, '%') " +
            "</if>" +
            "<if test='query.targetPath != null and query.targetPath!=\"\"' >" +
            " and full_path like concat('%', #{query.targetPath}, '%') " +
            "</if>" +
            "<if test='query.status != null ' >" +
            " and status = #{query.status} " +
            "</if>" +
            "<if test='query.isSync != null ' >" +
            " and is_sync = #{query.isSync} " +
            "</if>" +
            "<if test='query.type != null ' >" +
            " and type = #{query.type} " +
            "</if>" +
            "<if test='query.userType != null ' >" +
            " and user_type = #{query.userType} " +
            "</if>" +
            "<if test='query.beginTime != null and query.beginTime != \"\" and query.endTime != null and query.endTime != \"\"'> " +
            " AND created_at  BETWEEN  #{query.beginTime} " +
            " AND #{query.endTime} " +
            "</if>" +
            " ORDER BY created_at desc " +
            "</script>")
    public List<ProjectMirrorDataVO> findPageList(Page page, @Param("query") MirrorPageQuery query);
}
